https://leetcode.cn/problems/satisfiability-of-equality-equations/submissions/501788886/

class Solution {
public:
    bool equationsPossible(vector<string>& equations) {
        vector<int> _ufs(26,-1);
        auto FindRoot=[&_ufs](int x)
        {
            while(_ufs[x]>=0)
            {
                x=_ufs[x];
            }
            return x;
        };
        //遍历方程 ==放在一起 
        for(int i=0;i<equations.size();i++)
        {
            //合并
            if(equations[i][1]=='=')
            {
                int root1=FindRoot(equations[i][0]-'a');
                int root2=FindRoot(equations[i][3]-'a');
                //避免 a==a 
                if(root1!=root2)
                {
                    _ufs[root1]+=_ufs[root2];
                    _ufs[root2]=root1;
                }
            }
        }

        for(int i=0;i<equations.size();i++)
        {
            //合并
            if(equations[i][1]=='!')
            {
                int root1=FindRoot(equations[i][0]-'a');
                int root2=FindRoot(equations[i][3]-'a');
                if(root1==root2)
                    return false;
            }
        }
        return true;
    }
};